package com.building.hcrm.wcf;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.util.Xml;

import com.building.hcrm.base.BaseServiceProxy;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.DateDeserializer;
import com.building.hcrm.common.HttpHelper;
import com.building.hcrm.model.ApkFileInfo;
import com.building.hcrm.model.DoSignResult;
import com.building.hcrm.model.DownloadUpdateFileQuery;
import com.building.hcrm.model.SignLog;
import com.building.hcrm.model.SignParam;
import com.building.hcrm.model.SignQuery;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.model.SysUserParam;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class UpdateServiceProxy extends BaseServiceProxy
{
	private static UpdateServiceProxy _instance;

	public static UpdateServiceProxy getInstance(Context context)
	{
		if (_instance == null)
		{
			_instance = new UpdateServiceProxy();
		}
		_instance._context = context;
		return _instance;
	}

	public ApkFileInfo GetUpdateFileInfo() throws Exception
	{
		ApkFileInfo re = null;
		GsonBuilder gsonb = new GsonBuilder();
		Gson gson = gsonb.create();
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "GetUpdateFileInfo",
				"");
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "GetUpdateFileInfoResult");
			for(String str : lstRe)
			{
				re = new ApkFileInfo();
				re = gson.fromJson(str, re.getClass());
			}
		}
		return re;
	}
	
	/**
	 * 获取相关账号签到情况 
	 * @param param 仅需要用户账号即可
	 * @return
	 * @throws Exception
	 */
	public String DownloadNextPackageFromFile(DownloadUpdateFileQuery param) throws Exception
	{
		String re = null;
		GsonBuilder gsonb = new GsonBuilder();
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		strPost  = String.format("{\"criteria\":%1$s}", strPost);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "DownloadNextPackageFromFile",
				strPost);
		if (ValidateResult(strReturn))
		{
			// 下载文件返回的是xml
			XmlPullParser xmlParser = Xml.newPullParser();
			xmlParser.setInput(new StringReader(strReturn));
			int evtType = xmlParser.getEventType();
			while (evtType != XmlPullParser.END_DOCUMENT) {
				switch (evtType) {
					case XmlPullParser.START_TAG :
						String tag = xmlParser.getName();
						if (tag.equalsIgnoreCase("DownloadNextPackageFromFileResult")) {
							re = xmlParser.nextText();
						}
						break;
				}
				evtType = xmlParser.next();
			}
		}
		return re;
	}
}
